package com.dhcc.core.framework.aop;

import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.dhcc.core.framework.annotion.CacheClear;
import com.dhcc.core.framework.cache.SysCache;

/**
 * 清除缓存的aop
 * 
 * @ClassName: CacheClearAop
 * @Description: TODO
 * @author: cyf
 * @date: 2018年3月22日 上午10:43:16
 */
@Aspect
@Component
public class CacheClearAop {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Pointcut(value = "@annotation(com.dhcc.core.framework.annotion.CacheClear)")
    private void cut() {

    }

    @Around("cut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {

        Signature signature = point.getSignature();
        MethodSignature methodSignature = null;
        if (!(signature instanceof MethodSignature)) {
            throw new IllegalArgumentException("该注解只能用于方法");
        }
        methodSignature = (MethodSignature) signature;

        Object target = point.getTarget();
        Method currentMethod = target.getClass().getMethod(methodSignature.getName(),
                methodSignature.getParameterTypes());

        CacheClear cacheClear = currentMethod.getAnnotation(CacheClear.class);
        if (cacheClear != null) {
            String values[] = cacheClear.value();
            for (String value : values) {
                SysCache.me().removeByCacheName(value);
            }
            log.debug("清空数据缓存！");
        }else{
            SysCache.me().removeAll();
        }

        return point.proceed();
    }

}
